﻿using NaireSystem.Api;
using NaireSystem.ViewModels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TYControls;
using NaireSystem.Datas;
using NaireSystem.DBEntity;
using NaireSystem.Enums;
using NaireSystem.Api.Dto;

namespace NaireSystem.Views
{

    public partial class CtrlLearnQts : TYUserControl
    {
        ViewModelLearnFiles vm = new ViewModelLearnFiles();
        public CtrlLearnQts()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += CtrlNaireQts_Loaded;
        }

        private async void CtrlNaireQts_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= CtrlNaireQts_Loaded;
            if (!await LoadUser())
            {
                MessageBox.Show("获取用户失败");
                return;
            }
            if (!await InitDictData())
            {
                return;
            }
            BtnClick_Search(null, null);
        }

        private async Task<bool> LoadUser()
        {
            vm.ListUser.Clear();
            var rData = await Run(() =>
            {
                return ServerApi.GetUserList();
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return false;
            }
            rData.data.ForEach(x =>
            {
                vm.ListUser.Add(new StringStringData { Key = x.s_ic_no, Value = x.s_name });
            });
            return true;
        }

        private void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.CurrentPage = 1;
            pager_CurrentPageChanged(null, null);

        }

        private void BtnClick_EditItem(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as LearnFileModel;
            ViewLearnEditQt view = new ViewLearnEditQt(dx.Data);
            view.ShowDialog();
            pager_CurrentPageChanged(null, null);
        }

        private async Task<bool> InitDictData()
        {
            var ret = await Run(() =>
            {
                return DictDataApi.GetList(new Tbl_DictData
                {
                    dict_type = $"{EnumDictType.naire_tag_type}"
                });
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return false;
            }
            vm.ListTag.Clear();
            foreach (var item in ret.data)
            {
                IntStringData model = new IntStringData { Key = item.dict_value, Value = item.dict_label };
                vm.ListTag.Add(model);
            }
            return true;
        }

        private async void BtnClick_DeleteItem(object sender, RoutedEventArgs e)
        {
            var model = (sender as Button).DataContext as QtModel;
            if (!this.ShowYesNo($"是否确认删除 ：{model.Title}"))
            {
                return;
            }
            var ret = await Run(() =>
            {
                return QtApi.DeleteQt(model.data.qtId);
            });

            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }

            pager_CurrentPageChanged(null, null);
        }

        private async void pager_CurrentPageChanged(object sender, Panuon.WPF.SelectedValueChangedRoutedEventArgs<int> e)
        {
            int pageIndex = pager.CurrentPage;
            LearnFileDto queryDto = new LearnFileDto()
            {
                creator_code = vm.SelectedUser?.Key,
                file_tag = vm.SelectTag?.Value,
                dtEnd = vm.DtEnd,
                dtStart = vm.DtStart,
                title = vm.Title,
            };
            vm.ListLearnFile.Clear();

            var rData = await Run(() =>
            {
                return LearnFileApi.ListFile(new PageInfo(pageIndex, 30), queryDto);

            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            pager.MaxPage = rData.page.PageCount();
            rData.data.ForEach(x =>
            {
                LearnFileModel fileModel = UIDataHelper.DataAsUI(x);
                fileModel.Index = (pageIndex - 1) * rData.page.PageSize + vm.ListLearnFile.Count + 1;
                vm.ListLearnFile.Add(fileModel);
            });
        }

        private void BtnClick_Add(object sender, RoutedEventArgs e)
        {
            //var dx = (sender as Button).DataContext as QtModel;
            ViewLearnEditQt view = new ViewLearnEditQt(null);
            view.ShowDialog();
            BtnClick_Search(null, null);
        }
    }
}
